﻿using scripts.net;
using scripts.proto;
using System;
using System.Collections.Generic;
using System.Text;

namespace scripts.logic
{
	public partial class MsgHandler
	{

		//同步位置协议
		public static void SyncCharacterMsg(ClientState c, BaseMsg msgBase)
		{
			SyncCharacterMsg msg = (SyncCharacterMsg)msgBase;
			Player player = c.player;
			if (player == null) return;
			//room
			Room room = RoomManager.GetRoom(player.roomId);
			if (room == null)
			{
				return;
			}
			//status
			if (room.status != Room.Status.FIGHT)
			{
				return;
			}
			//是否作弊
			if (Math.Abs(player.x - msg.x) > 5 ||
				Math.Abs(player.y - msg.y) > 5 ||
				Math.Abs(player.z - msg.z) > 5)
			{
				Console.WriteLine("疑似作弊 " + player.id);
			}
			//更新信息
			player.x = msg.x;
			player.y = msg.y;
			player.z = msg.z;
			player.ex = msg.ex;
			player.ey = msg.ey;
			player.ez = msg.ez;
			//广播
			msg.id = player.id;
			room.Broadcast(msg);
		}

		//开火协议
		public static void FireMsg(ClientState c, BaseMsg msgBase)
		{
			FireMsg msg = (FireMsg)msgBase;
			Player player = c.player;
			if (player == null) return;
			//room
			Room room = RoomManager.GetRoom(player.roomId);
			if (room == null)
			{
				return;
			}
			//status
			if (room.status != Room.Status.FIGHT)
			{
				return;
			}
			//广播
			msg.id = player.id;
			room.Broadcast(msg);
		}

		//击中协议
		public static void HitMsg(ClientState c, BaseMsg msgBase)
		{
			HitMsg msg = (HitMsg)msgBase;
			Player player = c.player;
			if (player == null) return;
			//targetPlayer
			Player targetPlayer = PlayerManager.GetPlayer(msg.targetId);
			if (targetPlayer == null)
			{
				return;
			}
			//room
			Room room = RoomManager.GetRoom(player.roomId);
			if (room == null)
			{
				return;
			}
			//status
			if (room.status != Room.Status.FIGHT)
			{
				return;
			}
			//发送者校验
			if (player.id != msg.id)
			{
				return;
			}
			//状态
			int damage = 35;
			targetPlayer.hp -= damage;
			//广播
			msg.id = player.id;
			msg.hp = player.hp;
			msg.damage = damage;
			room.Broadcast(msg);
		}

	}
}
